window.addEventListener('load', function () {
    // 横向动画函数
    function animate(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var step = (target - obj.offsetLeft) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (obj.offsetLeft == target) {
                clearInterval(obj.timer);
                callback && callback();
            }
            obj.style.left = obj.offsetLeft + step + 'px';
        }, 15);
    }
    // 清除小点点的样式，赋予新样式
    function clear_dian() {
        for (let i = 0; i < ol.children.length; i++) {
            ol.children[i].className = ''
        }
        ol.children[clean].className = 'cun'
    }
    // 轮播图模块
    // 鼠标经过，左右按钮显示，离开消失
    var fous_l = document.querySelector('.fous-l')
    var fous_r = document.querySelector('.fous-r')
    var fous = document.querySelector('.fous')
    fous.addEventListener('mousemove', function () {
        fous_l.style.display = 'block'
        fous_r.style.display = 'block'
        clearInterval(goback)
    })
    fous.addEventListener('mouseleave', function () {
        fous_l.style.display = 'none'
        fous_r.style.display = 'none'
        goback = setInterval(function () {
            fous_r.click()
        }, 2000)
    })

    // 动态生成小圆点
    var ul = fous.querySelector('ul')
    var ol = fous.querySelector('.quan')
    for (let i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li')
        ol.appendChild(li)
        ol.children[0].className = 'cun'
        li.addEventListener('click', function () {
            for (let j = 0; j < ol.children.length; j++) {
                ol.children[j].className = ''
            }
            this.className = 'cun'
            // 点击小圈圈，图片改变
            animate(ul, -i * fous.offsetWidth)
            num = clean = i
        })
    }
    var farst = ul.children[0].cloneNode(true)
    ul.appendChild(farst)

    // 点击右按钮，图片移动
    var num = 0
    var clean = 0
    // 节流阀
    var flag = true
    fous_r.addEventListener('click', function () {
        if (flag) {
            flag = false
            if (num == ul.children.length - 1) {
                ul.style.left = 0
                num = 0
            }
            num++
            animate(ul, -num * fous.offsetWidth, function () {
                flag = true
            })
            clean++
            if (clean == ol.children.length) {
                clean = 0
            }
            clear_dian()
        }
    })

    // 点击左按钮，图片移动
    fous_l.addEventListener('click', function () {
        if (flag) {
            flag = false
            if (num == 0) {
                num = ul.children.length - 1
                ul.style.left = -num * fous.offsetWidth + 'px'
            }
            num--
            animate(ul, -num * fous.offsetWidth, function () {
                flag = true
            })
            clean--
            if (clean < 0) {
                clean = ol.children.length - 1
            }
            clear_dian()
        }
    })

    // 自动播放
    var goback = this.setInterval(function () {
        fous_r.click()
    }, 2000)

    // 竖向动画函数
    function animater(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var step = (target - window.pageYOffset) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (window.pageYOffset == target) {
                clearInterval(obj.timer);
                callback && callback();
            }
            // obj.style.left = obj.offsetWidth + step + 'px';
            window.scroll(0, window.pageYOffset + step)
        }, 15);
    }

    // 使用jq 来添加楼层电梯模块
    // 滑动显示
    gitTool()
    var flag = true

    function gitTool() {
        if ($(this).scrollTop() >= $(".inter").offset().top) {
            $(".fixedtool").fadeIn()
        } else {
            $(".fixedtool").fadeOut()
        }
    }
    $(this).scroll(() => {
        gitTool()
        if (flag) {
            $(".floor .w").each(function (i, ele) {
                if ($(document).scrollTop() >= $(ele).offset().top) {
                    console.log(i);
                    $(".fixedtool li").eq(i).addClass("current").siblings().removeClass()
                }
            })
        }
    })
    // 点击切换楼层
    $(".fixedtool li").click(function () {
        flag = false
        let a = $(this).index()
        if (a == 4) {
            $("body,html").stop().animate({
                scrollTop: 0
            }, function () {
                flag =true
                gitTool()
            })
        } else {
            var currt = $(".floor .w").eq(a).offset().top
            $("body,html").stop().animate({
                scrollTop: currt
            }, function () {
                flag =true
            })
        }
        $(this).addClass("current").siblings().removeClass()
    })
})